package com.sijia.reggie.controller;

import com.sijia.reggie.comment.R;
import com.sijia.reggie.entity.User;
import com.sijia.reggie.service.UserService;
import com.sijia.reggie.utils.SMSUtils;
import com.sijia.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;


/**
 * @author 杨思佳
 * @version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService us;



    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session) {
        String phone = user.getPhone();

        if (phone.isEmpty()) {
            String code = ValidateCodeUtils.generateValidateCode(6).toString();
            log.info(code);

            SMSUtils.sendMessage("reggie", "SMS_271465353", phone, code);

            return R.success("验证码发送成功");
        }
        return R.error("验证码发送失败");
    }


    @PostMapping("/login")
    public R<User> login(@RequestBody User u, HttpSession session) {
        String phone = (String) u.getPhone();
        User user = new User();
        user.setPhone(phone);
        session.setAttribute("user", 1628417425050640386l);
        return R.success(user);
    }
}
